<!DOCTYPE html>
<html>

<head>
    <title>JSMS PhoneGap Demo</title>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    <meta name="msapplication-tap-highlight" content="no"/>
    <meta name="viewport" content="user-scalable=no, initial-scale=1,
        maximum-scale=1, minimum-scale=1, width=device-width,
        height=device-height, target-densitydpi=device-dpi"/>
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,700">
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript">
        var time = 30   // 默认倒计时 30 秒。
        var t
        var phoneNum

        function onDeviceReady() {
            window.jsms.init()
        }

        var getSmsCode = function () {
            phoneNum = document.getElementById('phoneNum').value

            window.jsms.getSmsCode(uuid => {    // uuid 为本次操作的唯一标识。
                console.log(uuid)
                countDown()

            }, errorCode => {
                console.log(errorCode)

            }, phoneNum, '1')
        }

        function checkSmsCode() {
            var code = document.getElementById('smsCode').value

            window.jsms.checkSmsCode(successMsg => {
                alert(successMsg)
                clearTimeout(t)
                document.getElementById('time').value = time

            }, error => {
                alert(error.description)

            }, phoneNum, code)
        }

        function countDown() {
            if (time > 0) {
                time--
                document.getElementById('time').value = time
                t = setTimeout("countDown()", 1000)
            }
        }

        document.addEventListener('deviceready', onDeviceReady, false)
    </script>
</head>

<body>
<label>
    <br>
    <input id="phoneNum" type="text" placeholder="手机号">
    <input id="time" value=30><br><br>
    <button onclick="getSmsCode()">获取验证码</button><br>
</label>
<br>
<label>
    <input id="smsCode" type="text" placeholder="验证码"/>
    <button onclick="checkSmsCode()">验证</button>
</label>
</body>

</html>
